<?php

namespace App\Http\Controllers\Admin;

use App\Exports\FangownerExport;
use App\Http\Requests\FangownerRequest;
use App\Jobs\CreateExcelFangowner;
use App\Models\Fangowner;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;

use Mail;
use Illuminate\Mail\Message;

class FangownerController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Fangowner $fangowner)
    {
        $data = Fangowner::orderBy("id","basc")->paginate($this->pagsize);
        return view("admin.fangowner.index",compact("data","fangowner"));
    }

    # 显示添加房东页面
    public function create()
    {
        return view("admin.fangowner.create");
    }

    # 添加房东数据处理
    public function store(FangownerRequest $request)
    {
        $data = $request;
//        dump($data);
        $model = Fangowner::create($data->except(["_token","file"]));
//        if($model){
//            Mail::raw("恭喜您在本平台添加成功，我们后续为您服务",function(Message $message) use ($model){
//                $message->to($model->email,$model->name);
//                $message->subject("添加成功通知");
//            });
//        }
        return redirect( route("admin.fangowner.index"))->with("success","添加成功");
    }

    # 查看身份证
    public function show(Fangowner $fangowner)
    {
//        $pic = trim($fangowner,"#");
//        $fangowner = explode("#",$pic);
//        dump($fangowner);die;
        return view("admin.fangattr.show",compact("fangowner"));
    }

    # 修改显示
    public function edit(Fangowner $fangowner)
    {
//        dump($fangowner);
        return view("admin.fangowner.edit",compact("fangowner"));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Fangowner  $fangowner
     * @return \Illuminate\Http\Response
     */
    public function update(FangownerRequest $request, Fangowner $fangowner)
    {
//        dump($request);die;
        $data = $request->except(["_token","_method","file"]);
        $fangowner->update($data);
        return redirect(route("admin.fangowner.index"))->with("success","修改成功");
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Fangowner  $fangowner
     * @return \Illuminate\Http\Response
     */
    public function destroy(Fangowner $fangowner)
    {
        //
    }
    # 删除身份证图片
    public function deletePic(Request $request){
        $path = $request->get("path");
        $id = $request->get("id");
        $pic = $request->get("pic");
        #　删除文件操作
        $filepath = public_path($path);
        if(file_exists($filepath)){
            # 删除图片
            unlink($filepath);
        }
        # 修改数据字段里面的值
        $model = Fangowner::find($id);
        # 修改字段值
        $data['pic'] = str_replace("#".$path,"",$pic);
        # 修改数据表数据
        $model->update($data);
        return ["status" => 200,"msg" => "修改成功","pic" => $data["pic"]];
    }
    # 导出export文件
    public function excel(){
        return Excel::download(new FangownerExport(),"fangowner.xlsx");
    }
    # 生成Excel文件
    public function createexcel(){
            $this->dispatch(new CreateExcelFangowner());
            return redirect(route("admin.fangowner.index"));
    }

}
